import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.*;

import java.io.File;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

/**
 * http://my.oschina.net/zzq350623/blog/519542
 * <p>
 * 格式转换
 * 转换成xls 需要 org.apache.poi 库的支持
 */
public class PdfXls {
    private static final String PREFIX = "/jrxml/ds_jdbc";
    private static File DIR = null;
    static String jdbcUrl = "jdbc:mysql://104.236.132.55:3306/appbricks?useUnicode=true&characterEncoding=utf-8&useSSL=false";
    static String username = "root";
    static String password = "ab_mysql_v3";

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            DIR = new File(PdfXls.class.getResource("/").toURI());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void compile() throws Exception {
        System.out.println("start compile");
        JasperCompileManager.compileReportToFile(DIR + PREFIX + ".jrxml");
        System.out.println("end compile");

    }

    /**
     * 将数据填充到编译后的jasper，重新生成一个jrprint的文件
     *
     * @throws Exception
     */
    public static void fillData() throws Exception {
        System.out.println("start fillData");
        Map<String, Object> params = new HashMap<String, Object>();
        Connection con = DriverManager.getConnection(jdbcUrl, username, password);
        JasperFillManager.fillReportToFile(DIR + PREFIX + ".jasper", params, con);
        System.out.println("end fillData");
    }

    /**
     * 将报表导出成pdf
     *
     * @throws Exception
     */
    public static void printPDF() throws Exception {
        System.out.println("start printPDF");
        JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(new File(DIR + PREFIX + ".jrprint"));
        File destFile = new File(DIR + PREFIX + ".pdf");
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
        exporter.exportReport();
        System.out.println("end printPDF");
    }

    /**
     * 打印成excel文件
     *
     * @throws Exception
     */
    public static void printXls() throws Exception {
        System.out.println("start printXls");
        JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(new File(DIR + PREFIX + ".jrprint"));
        File destFile = new File(DIR + PREFIX + ".xls");
        JRXlsExporter exporter = new JRXlsExporter();

        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
        SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
        configuration.setOnePagePerSheet(false);
        exporter.setConfiguration(configuration);

        exporter.exportReport();
        System.out.println("end printXls");
    }

    /**
     * 打印成excel文件
     *
     * @throws Exception
     */
    public static void printHtml() throws Exception {
        System.out.println("start printHtml");
        JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(new File(DIR + PREFIX + ".jrprint"));
        JasperExportManager.exportReportToHtmlFile(jasperPrint, DIR + PREFIX + ".html");
        System.out.println("end printHtml");
    }

    public static void main(String[] args) throws Exception {
//        compile();
//        fillData();
//        printPDF();
//        printXls();
        printHtml();
    }
}